//
//  CompleteVipCardInfoViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/31.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "CompleteVipCardInfoViewController.h"
#import "CompleteVipCardInfoView.h"
#import "SelectWorkViewController.h"
#import "ChosePayView.h"
#import "VipCardViewController.h"

@interface CompleteVipCardInfoViewController ()

@property(nonatomic,weak)CompleteVipCardInfoView *completeView;

@end

@implementation CompleteVipCardInfoViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"完善信息";
    
    [self createSubView];
}

-(void)createSubView{
    
    CompleteVipCardInfoView *completeView = [[CompleteVipCardInfoView alloc] init];
    completeView.selectRow = self.selectRow;
    [self.view addSubview:completeView];
    self.completeView = completeView;
    [completeView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
    
    //赋值
    if ([self.commonCard.IsLimit isEqualToString:@"Y"]) {
        
        completeView.cardNumLabel.text = [NSString stringWithFormat:@"可提前预约%@节课、%@、每个会员限购%@张",self.commonCard.BeforeTimes,self.commonCard.UnitTimes,self.commonCard.LimitCards];
    }
    else{
        
        completeView.cardNumLabel.text = @"没有限制";
    }
    
    //实收
    completeView.moneyLabel.text = [NSString stringWithFormat:@"%@元",[self.dataDict objectForKey:@"FactFee"]];
    
    /**
     地址
     */
    NSString *addrStr = @"";
    
    for (NSString *name in self.commonCard.ShopName) {
        
        addrStr = [NSString stringWithFormat:@"%@%@、",addrStr,name];
    }
    addrStr = addrStr?[addrStr substringWithRange:NSMakeRange(0, addrStr.length - 1)]:@"";
    
    completeView.vipNameLabel.text = addrStr;
    
   
    if (self.cardSpec) {
        
        //期限
        BOOL IsValidityDay = [[self.cardSpec objectForKey:@"IsValidityDay"] boolValue];
        
        //有设置期限
        if (IsValidityDay) {
            
            completeView.validLabel.text = [NSString stringWithFormat:@"有效期:%@天",[self.cardSpec objectForKey:@"ValidityDay"]];
        }
        else{
            
            completeView.validLabel.text = @"没有期限";
        }
    }
    else{
     
        //其他规格 要自己算有效期
        if (![[self.dataDict objectForKey:@"JoinTime"] isEmptyString]) {
            
            completeView.validLabel.text = [NSString stringWithFormat:@"有效期:%@至%@",[self.dataDict objectForKey:@"JoinTime"],[self.dataDict objectForKey:@"EndTime"]];
        }
        else{
            
            completeView.validLabel.text = @"没有有效期";
        }
    }
        
    //次卡
    if ([self.commonCard.Type intValue] == Time) {
        
        if (self.cardSpec) {
         
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%@次",[self.cardSpec objectForKey:@"Times"]];
            
            //次卡选了规格的话 要从新赋值
            [self.dataDict setValue:[self.cardSpec objectForKey:@"Times"] forKey:@"Times"];
        }
        else{
            
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%@次",[self.dataDict objectForKey:@"Times"]];
        }
        
        completeView.cardClassNameLabel.text = [NSString stringWithFormat:@"次卡类型(%@)",self.commonCard.ID];
    }
    //期限卡
    else if ([self.commonCard.Type intValue] == Term) {
        
        if (self.cardSpec) {
         
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%@天",[self.cardSpec objectForKey:@"Times"]];
        }
        else{
            
            NSDateComponents *dataCom = [NSString getDateDifferenceWithStartDate:[self.cardSpec objectForKey:@"JoinTime"] andDate:[self.cardSpec objectForKey:@"EndTime"]];
            
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%ld天",(long)dataCom.day];
        }
        
        completeView.cardClassNameLabel.text = [NSString stringWithFormat:@"期限类型(%@)",self.commonCard.ID];
        
    }
    //储值
    else{
        
        if (self.cardSpec) {
         
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%@元",[self.cardSpec objectForKey:@"Times"]];
        }
        else{
            
            completeView.balanceDetailLabel.text = [NSString stringWithFormat:@"%@元",[self.dataDict objectForKey:@"TotalFee"]];
            
        }
        
        completeView.cardClassNameLabel.text = [NSString stringWithFormat:@"储值类型(%@)",self.commonCard.ID];
    }
    
    
    //选择销售
    [completeView.salBtnView.choseBtn addTarget:self action:@selector(choseSaleMan)];
    
    
    //选择教练
    [completeView.coachBtnView.choseBtn addTarget:self action:@selector(choseCoaches)];
    
    //介绍人
//    [completeView.introduceBtnView.choseBtn addTarget:self action:@selector(choseIntroduce)];
    
    //支付方式
    [completeView.payBtnView.choseBtn addTarget:self action:@selector(payBtnClick)];
    
    //确认按钮
    [completeView.sureBtn addTarget:self action:@selector(sureBtnClick)];
}


/**
 选择教练
 */
-(void)choseCoaches{
    
    __weak typeof(self)weakSelf = self;
    
    SelectWorkViewController *selectWorkVC = [[SelectWorkViewController alloc] init];
    
    selectWorkVC.from = @"教练";
    
    selectWorkVC.sendSelectWorkerBlock = ^(NSString *workId, NSString *workName) {
    
        weakSelf.completeView.coachBtnView.choseLabel.text = workName;
        weakSelf.completeView.coachBtnView.choseLabel.textColor = TFColor;
        [weakSelf.dataDict setObject:workId forKey:@"Coach"];
    };
    
    [self.navigationController pushViewController:selectWorkVC animated:YES];
}

/**
 选择销售
 */
-(void)choseSaleMan{
    
    __weak typeof(self)weakSelf = self;
    
    SelectWorkViewController *selectWorkVC = [[SelectWorkViewController alloc] init];
    
    selectWorkVC.from = @"销售顾问";
    
    selectWorkVC.sendSelectWorkerBlock = ^(NSString *workId, NSString *workName) {
        
        weakSelf.completeView.salBtnView.choseLabel.text = workName;
        weakSelf.completeView.salBtnView.choseLabel.textColor = TFColor;
        [weakSelf.dataDict setObject:workId forKey:@"Saler"];
    };
    
    [self.navigationController pushViewController:selectWorkVC animated:YES];
}

#pragma mark 介绍人
-(void)choseIntroduce{
    
    __weak typeof(self)weakSelf = self;
    
    SelectWorkViewController *selectWorkVC = [[SelectWorkViewController alloc] init];
    
    selectWorkVC.from = @"";
    
    selectWorkVC.sendSelectWorkerBlock = ^(NSString *workId, NSString *workName) {
        
        //weakSelf.completeView.introduceBtnView.choseLabel.text = workName;
        //weakSelf.completeView.introduceBtnView.choseLabel.textColor = TFColor;
        [weakSelf.dataDict setObject:workId forKey:@"Introducer"];
    };
    
    [self.navigationController pushViewController:selectWorkVC animated:YES];
    
}

#pragma mark 支付方式选择
-(void)payBtnClick{
    
    __weak typeof(self)weakSelf = self;
    
    ChosePayView *chosePayView = [[ChosePayView alloc] init];
    
    chosePayView.commitBlock = ^(NSString *str,NSString *strId) {
        
        weakSelf.completeView.payBtnView.choseLabel.text = str;
        weakSelf.completeView.payBtnView.choseLabel.textColor = TFColor;
        [weakSelf.dataDict setObject:strId forKey:@"PayType"];
    };
    
    [chosePayView showWithAnimation];
}

#pragma mark 保存按钮点击
-(void)sureBtnClick{
    
    //会员卡号
    NSString *cardNo = self.completeView.vipCardInputView.titleTF.text;
    
    if ([cardNo isEmptyString] || ![self.dataDict objectForKey:@"Saler"] ||
        ![self.dataDict objectForKey:@"Coach"]) {
        
        [MBProgressHUD showText:@"请输入必填项"];
        
        return;
    }
    
    [self.dataDict setObject:cardNo forKey:@"MemberCardNum"];
    
    //介绍人
    [self.dataDict setValue:self.completeView.introduceBtnView.titleTF.text forKey:@"Introducer"];
    
    //备注
    [self.dataDict setValue:self.completeView.remarkBtnView.titleTF.text forKey:@"Memo"];
    
    NSString *isOld = self.completeView.hisSwitchView.titleSwitch.isOn?@"1":@"0";
    
    //是否历史订单
    [self.dataDict setValue:isOld forKey:@"IsOld"];
    
    [self.dataDict setValue:self.completeView.hisTime forKey:@"CreateTime"];
    
    
    //Post_SalesOrderOperate
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [self.dataDict setObject:venueModel.venueId forKey:@"ShopID"];
//    
//    [self.dataDict setObject:self.commonCard.ID forKey:@"CardType"];
    
    [HTTPTool POSTRaw:Post_SalesOrderOperate parameters:self.dataDict
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"添加成功"];
                      
                      [NotificationCenter postNotificationName:@"ReloadVenueCard" object:nil];
                      
                  
                      for (UIViewController *tmpVC in self.navigationController.viewControllers) {
                          if ([tmpVC isKindOfClass:[VipCardViewController class]]) {
                              [self.navigationController popToViewController:tmpVC animated:YES];
                          }
                      }
                      
                      //[self.navigationController popViewControllerAnimated:YES];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
}
@end
